*&---------------------------------------------------------------------*
*& Report YBC_SWS_DEMO01
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT YBC_SWS_DEMO01.

DATA:
  LS_ESSR    TYPE ESSR,
  LT_ESLL_UP TYPE STANDARD TABLE OF ESLL,
  LT_ESLL    TYPE STANDARD TABLE OF ESLL.

DATA:
  LV_AMOUNT    TYPE P DECIMALS 2,
  LV_BUSID     TYPE YBC_SWS_REQUEST-BUSINESSID,
  LS_HEAD_DATA TYPE YBCS_SWS_DATA_H,
  LS_ITEM_DATA TYPE YBCS_SWS_DATA_I,
  LT_HEAD_DATA TYPE STANDARD TABLE OF YBCS_SWS_DATA_H,
  LT_ITEM_DATA TYPE STANDARD TABLE OF YBCS_SWS_DATA_I,
  LS_APPROVER  TYPE YBCS_SWS_APPROVER,
  LT_APPROVER  TYPE STANDARD TABLE OF YBCS_SWS_APPROVER,
  LS_ATTACH    TYPE YBCS_SWS_ATTACH01,
  LT_ATTACH    TYPE YBCT_SWS_ATTACH01,
  LS_MSG       TYPE BAPIRET2,
  LV_MESSAGE   TYPE STRING.

SELECT-OPTIONS:
  S_LBLNI FOR LS_ESSR-LBLNI.

INITIALIZATION.

AT LINE-SELECTION.

START-OF-SELECTION.

  SELECT SINGLE *
    FROM ESSR
    INTO LS_ESSR
   WHERE LBLNI IN S_LBLNI.


  SELECT *
    FROM ESLL
    INTO TABLE LT_ESLL_UP
   WHERE PACKNO = LS_ESSR-PACKNO.

  IF SY-SUBRC = 0.
    SELECT *
      FROM ESLL
      INTO TABLE LT_ESLL
       FOR ALL ENTRIES IN LT_ESLL_UP
     WHERE PACKNO = LT_ESLL_UP-SUB_PACKNO.
  ENDIF.

  LV_BUSID = LS_ESSR-LBLNI.

  CALL FUNCTION 'YBC_SWS_CANCEL_REQUEST'
    EXPORTING
*     IV_REQUESTID      =
      IV_APPID          = 'ZSWS_APP01'
      IV_BUSINESSID     = LV_BUSID
      IV_NO_EXIST_CHECK = 'X'
*     IV_NO_STATUS_CHECK       =
*   IMPORTING
*     ES_MSG            =
    .

  "根据结构定义，自动赋值抬头行项目值
  CALL FUNCTION 'YBC_SWS_PREPARE_DATA'
    EXPORTING
      IV_HEADER    = LS_ESSR
    IMPORTING
      ES_MSG       = LS_MSG
    TABLES
      IT_ITEM      = LT_ESLL
      ET_HEAD_DATA = LT_HEAD_DATA
      ET_ITEM_DATA = LT_ITEM_DATA.

  "添加自定义字段，不在ESSR结构中
  CLEAR LS_HEAD_DATA.
  LS_HEAD_DATA-VALUE = '自定义字段值'.
  LS_HEAD_DATA-FIELD = 'ZZADD'.
  CONDENSE LS_HEAD_DATA-VALUE.
  APPEND LS_HEAD_DATA TO LT_HEAD_DATA.

  WRITE: / 'PREPARE_DATA' , LS_MSG-MESSAGE.

  IF LS_MSG-TYPE <> 'E'.

    CALL FUNCTION 'YBC_SWS_SEND_FIRST'
      EXPORTING
        IV_APPID      = 'ZSWS_APP01'
        IV_TEMPLID    = 'ZSWS_APP01'
        IV_BUSINESSID = LV_BUSID
*       IV_UNAME      = SY-UNAME
      IMPORTING
        ES_MSG        = LS_MSG
      TABLES
        IT_HEAD_DATA  = LT_HEAD_DATA
        IT_ITEM_DATA  = LT_ITEM_DATA
*       IT_APPROVER   =
*       IT_ATTACH     =
      .
  ENDIF.

  WRITE: / 'Send First', LS_MSG-MESSAGE.
